package com.secondary.dao.goods.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;

import com.secondary.dao.BaseDao;
import com.secondary.dao.goods.GoodsDao;
import com.secondary.pojos.Goods;
@Component("goodsDao")
public class GoodsDaoImpl extends BaseDao implements GoodsDao{

	@Override
	public Goods findGoodById(int id) {
		
		return (Goods)findObject(Goods.class,id);
	}

	@Override
	public List<Goods> findGoodsByParentsId(int id) {
		//Set<Goods> set=((Category)findObject(Category.class,id)).getGoods();
		List<Goods> goodList=getSession().createQuery("select new Goods(g.id,name,g.startTime,g.seller,g.quality)from Goods g where "+id+" in (select c.id from g.categorys c)").list();
		
		return goodList;
//		List<Goods> goodList=getSession().createQuery("select new Goods(id,name,user)from Goods where category.id="+id).list();
//		System.out.println("|||||||||||||"+goodList.size());
//		return goodList;
	}

	@Override
	public List<Goods> findGoodsByName(String name) {
		return getSession().createQuery("select new Goods(g.id,name,g.startTime,g.seller,g.quality)from Goods g where g.name like :name").setParameter("name", "%"+name+"%").list();
		
	}

	@Override
	public void saveObject(Object o) {
		
		super.saveObject(o);
	}

	@Override
	public List<Goods> findGoodsByUserId(int id) {
		List<Goods> goodList=getSession().createQuery("select new Goods(g.id,g.name,g.state,g.startTime,g.postDays) from Goods g where g.seller.id="+id).list();
		return goodList;
	}

	@Override
	public void deleteGoods(List<Integer> id) {
		Query query=getSession().createQuery("delete Goods g where g.id in (:ids)");
		query.setParameterList("ids", id);
		query.executeUpdate();
		
	}

	@Override
	public void deletePic(int id) {
		// TODO Auto-generated method stub
		getSession().createQuery("delete GoodsPic where id=:id").setParameter("id", id).executeUpdate();
	}

	@Override
	public void updateGoodsInfo(Goods good) {
		System.out.println("updateGoodsInfo");
		Session session=getSession();
		System.out.println("______________ssadasdasdsadasd_______"+good.getContent());
		Query query=session.createQuery("update Goods set name=:name,content=:content,quality=:quality,postDays=:postDays," +
				"contact=:contact,email=:email,phone=:phone,state=0,address=:address,qq=:qq where id=:id");
		query.setParameter("name", good.getName());
		query.setParameter("content", good.getContent());
		query.setParameter("quality", good.getQuality());
		query.setParameter("postDays", good.getPostDays());
		query.setParameter("contact", good.getContact());
		query.setParameter("email", good.getEmail());
		query.setParameter("phone", good.getPhone());
		query.setParameter("address", good.getAddress());
		query.setParameter("qq", good.getQq());
		query.setParameter("id", good.getId());
		int a=query.executeUpdate();
		System.out.println("++++++++++++++++++++++++++__"+a);
	}
	
	
	
}
